local hujian = fk.CreateSkill {
  name = "hujian",
}

local hujian_func={
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.GameStart then
        return true
      else
        if table.find(player.room.discard_pile, function(id) return Fk:getCardById(id).trueName == "blood_sword" end) then
          local room = player.room
          local e = room.logic:getCurrentEvent()
          local end_id = e.id
          local id = 0
          local events = room.logic.event_recorder[GameEvent.UseCard] or Util.DummyTable
          for i = #events, 1, -1 do
            e = events[i]
            if e.id <= end_id then break end
            end_id = e.id
            local use = e.data[1]
            id = use.from
          end
          events = room.logic.event_recorder[GameEvent.RespondCard] or Util.DummyTable
          for i = #events, 1, -1 do
            e = events[i]
            if e.id <= end_id then break end
            end_id = e.id
            local response = e.data[1]
            id = response.from
          end
          if id ~= 0 and not room:getPlayerById(id).dead then
            event:setCostData(self,id)
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.GameStart then
      return true
    else
      return player.room:askForSkillInvoke(player.room:getPlayerById(event:getCostData(self)), self.name, nil, "#hujian-invoke")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local card = room:printCard("blood_sword", Card.Spade, 6)
      room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true, player.id)
    else
      local p = room:getPlayerById(event:getCostData(self))
      local card = room:getCardsFromPileByRule("blood_sword", 1, "discardPile")
      if #card > 0 then
        room:moveCardTo(card, Card.PlayerHand, p, fk.ReasonJustMove, self.name, nil, true, p.id)
      end
    end
  end,
}

hujian:addEffect(fk.GameStart, hujian_func)
hujian:addEffect(fk.TurnEnd, hujian_func)

return hujian